/**
 * 
 */
package co.fitstart.api.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.demo2do.core.persistence.GenericDaoSupport;

import co.fitstart.entity.course.schedule.CourseSchedule;
import co.fitstart.entity.course.schedule.ScheduleStatus;
import co.fitstart.entity.credit.Credit;
import co.fitstart.entity.order.Order;
import co.fitstart.entity.order.PaymentStatus;
import co.fitstart.entity.user.Student;

/**
 * @author downpour
 *
 */
@Repository("studentDaoSupport")
public class StudentDaoSupport {
    
    @Autowired
    private GenericDaoSupport genericDaoSupport;

    /**
     * 
     * @return
     */
    @SuppressWarnings("unchecked")
    public List<Order> listAllPaidOrder() {
        String sentence = "FROM Order order WHERE paymentStatus = :paymenStatus AND disabled = false";
        return genericDaoSupport.searchForList(sentence, "paymenStatus", PaymentStatus.PAID);
    }

    /**
     * 
     * @param userId
     * @return
     */
    public Student getStudentByUserId(Long userId) {
        String sentence = "FROM Student student WHERE student.user.id = :userId";
        return genericDaoSupport.searchForObject(sentence, "userId", userId, Student.class);
    }
    
    /**
     * 
     * @param userId
     * @return
     */
    public Credit getUserCredit(Long userId) {
        String sentence = "FROM Credit credit WHERE credit.user.id = :userId";
        return genericDaoSupport.searchForObject(sentence, "userId", userId, Credit.class);
    }
    
    /**
     * list experience course schedule
     * @param userId
     * @return
     */
    @SuppressWarnings("unchecked")
    public List<CourseSchedule> listFinishedExperienceSchedule(Long userId) {
        String sentence = "FROM CourseSchedule WHERE disabled = false"
                + " AND student.user.id = :userId AND course.experience = :experience"
                + " AND scheduleStatus = :finish";
        
        Map<String, Object> parameters = new HashMap<>();
        parameters.put("userId", userId);
        parameters.put("experience", true);
        parameters.put("finish", ScheduleStatus.FINISH);
        
        return genericDaoSupport.searchForList(sentence, parameters);
    }
}
